package com.lebang.controller.sys.cences;

import com.lebang.common.Login;
import com.lebang.model.cences.OsCencesModel;
import com.lebang.service.buss.MemberService;
import com.lebang.service.cences.CencesOsOsvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 机型人数统计
 *
 * @author xwj
 * @date 2020-04-27
 */
@Controller
@RequestMapping("/sys/cences/cencesososv")
public class CencesOsOsvController {
    @Autowired
    private CencesOsOsvService baseService;
    @Autowired
    private MemberService memberService;


    @Login
    @GetMapping("/list")
    public String list() {
        return "/cencesososv/list";
    }

    /**
     * @param star 开始日期
     * @param end  结束日期
     * @return 返回数据
     */
    @PostMapping("/page")
    @ResponseBody
    @Login
    public HashMap newList(@RequestParam(value = "star", required = false) String star
            , @RequestParam(value = "end", required = false) String end
            , @RequestParam(value = "brand", required = false) String brand
            , @RequestParam(value = "osVersion", required = false) String osVersion
            , @RequestParam(value = "osv", required = false) String osv
            , @RequestParam(value = "type", required = false) Integer type
            , @RequestParam(value = "memberType", required = false) Integer memberType) {
        HashMap map = new HashMap(1);
        map.put("code", 0);
        if (star == null) {
            map.put("data", new ArrayList<OsCencesModel>());
            map.put("count", 0);
            map.put("all", 0);
            return map;
        }

        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime starTime = LocalDateTime.parse(star, fmt);
        LocalDateTime endTime = LocalDateTime.parse(end, fmt);

        if (type == 1) {

            // List<>
            //查询机型
            if (memberType == 1) {
                //活跃人数
                List<OsCencesModel> osvActiveList = memberService.countOsActive(starTime, endTime, brand, osv);
                Integer all = baseService.countAll(osvActiveList);
                map.put("all", all);
                map.put("data", osvActiveList);
                map.put("count", osvActiveList.size());
            } else {
                //新增人数
                List<OsCencesModel> osvRegisterList = memberService.countOsRegister(starTime, endTime, brand, osv);
                Integer all = baseService.countAll(osvRegisterList);
                map.put("all", all);
                map.put("data", osvRegisterList);
                map.put("count", osvRegisterList.size());
            }


        } else {
            //查询系统版本
            if (memberType == 1) {
                //活跃人数
                List<OsCencesModel> osVersionActiveList = memberService.countOsVersionActive(starTime, endTime, brand, osVersion);
                Integer all = baseService.countAll(osVersionActiveList);
                map.put("all", all);
                map.put("data", osVersionActiveList);
                map.put("count", osVersionActiveList.size());
            } else {
                //新增人数
                List<OsCencesModel> osVersionRegisterList = memberService.countOsVersionRegister(starTime, endTime, brand, osVersion);
                Integer all = baseService.countAll(osVersionRegisterList);
                map.put("all", all);
                map.put("data", osVersionRegisterList);
                map.put("count", osVersionRegisterList.size());
            }

        }

        return map;

    }


}
